﻿using HualongExcel.Enum;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;

namespace HualongExcel.Utilities
{
    public static class VariableProcessor
    {
        /// <summary>
        /// 变量：时间 年-月-日
        /// </summary>
        public static string Var_Time
        {
            get {
                return string.Format("{0:D}", DateTime.Now);
            }
        }
        /// <summary>
        /// 变量：年份
        /// </summary>
        public static string Var_Year
        {
            get {
                return DateTime.Now.Year.ToString();
            }
        }
        /// <summary>
        /// 变量：月份
        /// </summary>
        public static string Var_Month
        {
            get
            {
                return DateTime.Now.Month.ToString();
            }
        }
        /// <summary>
        /// 变量: 星期
        /// </summary>
        public static string Var_Week
        {
            get {

                switch (DateTime.Now.DayOfWeek.ToString().ToLower())
                {
                    case "monday":
                        return "一";
                    case "tuesday":
                        return "二";
                    case "wednesday":
                        return "三";
                    case "thursday":
                        return "四";
                    case "friday":
                        return "五";
                    case "saturday":
                        return "六";
                    case "sunday":
                        return "天";
                }
                return string.Empty;
            }
        }
        /// <summary>
        /// 变量 日
        /// </summary>
        public static string Var_Day
        {
            get
            {
                return DateTime.Now.Day.ToString();
            }
        }
        /// <summary>
        /// 变量 小时
        /// </summary>
        public static string Var_Hour
        {
            get
            {
                return DateTime.Now.Hour.ToString();
            }
        }
        /// <summary>
        /// 变量 分钟
        /// </summary>
        public static string Var_Minute
        {
            get
            {
                return DateTime.Now.Minute.ToString();
            }
        }
        /// <summary>
        /// 变量 秒数
        /// </summary>
        public static string Var_Second
        {
            get
            {
                return DateTime.Now.Second.ToString();
            }
        }



        /// <summary>
        /// 变量生成次数
        /// </summary>
        public static string Var_GenerateCount
        {
            get {
                return Common.GenerateCount.ToString();
            }
        }

        /// <summary>
        /// 变量全局标识
        /// </summary>
        /// <returns></returns>
        public static string GenerateGuid()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
            {
                i *= ((int)b + 1);
            }
            return string.Format("{0:x}", i - DateTime.Now.Ticks).ToUpper();
        }

        public static string Process(string str)
        {
            str = str.ToLower();
            if (str.Contains("serialnumber")||str.Contains("serialnum"))
            {
                var pattern = @"[0-9]+";
                var match= Regex.Match(str, pattern);
                if (match.Success)
                {
                    return Var_GenerateCount.ToString().PadLeft(int.Parse(match.Value), '0');
                }
            }

            var cellAddressMatch = Regex.Match(str, @"(\$[a-zA-Z]+\$[1-9]+)");
            if(cellAddressMatch.Success)
            {

                string source_address = cellAddressMatch.Value;

                var ColumnStartIndex = source_address.IndexOf("$");
                var RowStartIndex = source_address.LastIndexOf("$");
                var Column_Address = source_address.Substring(ColumnStartIndex + 1, RowStartIndex - 1);
                var Row_Address = source_address.Substring(RowStartIndex + 1);

                switch (Common.SelectedReplaceType)
                {
                    case ReplaceType.WithRow://跟随当前行
                        Row_Address = Common.SelectedRow.ToString();
                        break;
                    case ReplaceType.WithColumn://跟随当前列
                        Column_Address = Common.SelectedColumn.ToName();
                        break;
                }

                source_address = $"{Column_Address}{Row_Address}";

                return ExcelHelper.GetCellContent(source_address);
            }

            switch (str)
            {
                case "time":
                    return Var_Time;
                case "year":
                    return Var_Year;
                case "month":
                    return Var_Month;
                case "week":
                    return Var_Week;
                case "day":
                    return Var_Day;
                case "hour":
                    return Var_Hour;
                case "minute":
                    return Var_Minute;
                case "second":
                    return Var_Second;
                case "generatecount":
                    return Var_GenerateCount;
                case "guid":
                    return GenerateGuid();
            }
            return str;
        }


        public static string ReplaceVariable(this string str)
        {
            if (str == null)
                return str;
            var pattern = @"\{(.*?)\}";

           var matches= Regex.Matches(str, pattern);
            

            foreach (var item in matches)
            {
               var variableStr = item.ToString();
               variableStr= variableStr.Remove(0,1);
                variableStr = variableStr.Remove(variableStr.Length - 1, 1);

                str=str.Replace(item.ToString(), Process(variableStr)); 
                  
            }
            return str;
        }

 
    }



  
}
